<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	Authenticate
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Authenticate</h2>
    
	<img src="/Content/largeindicator.gif" /><br />
	<p>Please wait while we verify your identity...</p>


	<script type="text/javascript">

		$(function() {
			$.get('http://localhost:21259/SSOService.svc/user/RequestToken?callback=?', {},
				function(ssodata) {
					// get url to logon page in case this operation fails
					var logonPage = '<%=Url.Action("LogOn", "Account") %>';

					if (ssodata.Status == 'SUCCESS') {
						// get target url for successful authentication
						var redirect = '<%=Request["redirectUrl"] %>';
						if (redirect == '')
							redirect = '<%=Url.Action("Index", "Home") %>';

						// verify the token is genuine
						$.post('<%=Url.Action("Authenticate", "Account") %>',
							{ token: ssodata.Token, createPersistentCookie: true },
								function(data) {
									// redirect user based on result
									if (data.result == 'SUCCESS')
										document.location = redirect;
									else
										document.location = logonPage;
									// just regular json here
								}, 'json');
					} else {
						// user needs to logon to SSO service
						document.location = logonPage;
					}
					// tell jQuery to use JSONP 
				}, 'jsonp');
		});
		
	</script>

</asp:Content>
